package com.example.express_auth2.config;
import com.example.express_auth2.service.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {


    //加载用户详细信息
    @Autowired
    UserDetailsServiceImpl userDetailsService;


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        /**
         * Add authentication based upon the custom UserDetailsService that is passed in.
         * It then returns a DaoAuthenticationConfigurer to allow customization of the authentication.
         * This method also ensure that the UserDetailsService is available for the getDefaultUserDetailsService() method.
         * Note that additional UserDetailsService's may override this UserDetailsService as the default.
         */
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(bCryptPasswordEncoder);
    }

    /**
     * A HttpSecurity is similar to Spring Security's XML <http> element
     * in the namespace configuration. It allows configuring web based security for specific http requests.
     * By default it will be applied to all requests, but can be restricted using requestMatcher(RequestMatcher)
     * or other similar methods.
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                //.regexMatchers("/p1test").hasAnyAuthority("p1")
                .and().authorizeRequests().antMatchers("/**").permitAll()
                /*手机端使用，这些都不需要，只有网页需要
                .and().formLogin()//.loginPage("/login/page")
                .and().logout().permitAll()
                .and().rememberMe()*/;
    }

    /**
     * 设置资源访问规则，哪些可以访问，那些不可以访问
     * The WebSecurity is created by WebSecurityConfiguration to
     * create the FilterChainProxy known as the Spring Security Filter Chain
     * (springSecurityFilterChain). The springSecurityFilterChain is the Filter
     * that the DelegatingFilterProxy delegates to.
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/static/**");
    }
    //认证管理器
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    //密码编码器
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}
